import java.util.Timer;
import java.util.TimerTask;
import java.util.StringBuilder;

public class ControlPanel {
    public enum State { STANDBY, READING_PASSWORD, FUNCTION_SELECT, LOCKED }
    private State currentState;
    private final String correctPwd = "1234"; // 预设密码
    private StringBuilder inputPwd;
    private int errorCount;

    public ControlPanel() {
        currentState = State.STANDBY;
        inputPwd = new StringBuilder();
        errorCount = 0;
    }

    // 处理用户输入字符
    public void inputChar(char c) {
        switch (currentState) {
            case STANDBY:
                currentState = State.READING_PASSWORD;
                inputPwd.setLength(0);
                inputPwd.append(c);
                System.out.println("输入：" + c + "（长度：1）");
                break;
            case READING_PASSWORD:
                if (inputPwd.length() < 3) { // 未满4位
                    inputPwd.append(c);
                    System.out.println("输入：" + c + "（长度：" + (inputPwd.length() + 1) + "）");
                } else { // 第4位，验证密码
                    inputPwd.append(c);
                    System.out.println("输入完成：" + inputPwd);
                    verifyPwd();
                }
                break;
            case LOCKED:
                System.out.println("面板锁定，120秒后再试");
                break;
            case FUNCTION_SELECT:
                System.out.println("功能选择中，输入无效");
                break;
        }
    }

    // 密码验证逻辑
    private void verifyPwd() {
        if (inputPwd.toString().equals(correctPwd)) {
            currentState = State.FUNCTION_SELECT;
            errorCount = 0;
            System.out.println("密码正确 → 功能选择状态");
        } else {
            errorCount++;
            System.out.println("密码错误（" + errorCount + "次）");
            if (errorCount >= 3) {
                currentState = State.LOCKED;
                System.out.println("错误3次 → 锁定120秒");
                startLockTimer();
            } else {
                inputPwd.setLength(0); // 清空输入，重新读取
            }
        }
    }

    // 锁定计时
    private void startLockTimer() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                currentState = State.STANDBY;
                errorCount = 0;
                System.out.println("120秒超时 → 解锁，待机状态");
            }
        }, 120000); // 120秒
    }

    // 功能操作完成
    public void finishFunction() {
        if (currentState == State.FUNCTION_SELECT) {
            currentState = State.STANDBY;
            System.out.println("操作完成 → 待机状态");
        }
    }

    // 测试方法
    public static void main(String[] args) throws InterruptedException {
        ControlPanel panel = new ControlPanel();
        // 正确输入：1→2→3→4
        panel.inputChar('1');
        panel.inputChar('2');
        panel.inputChar('3');
        panel.inputChar('4');
        panel.finishFunction();
        // 错误输入3次
        panel.inputChar('0');
        panel.inputChar('0');
        panel.inputChar('0');
        panel.inputChar('0'); // 1次错误
        panel.inputChar('1');
        panel.inputChar('1');
        panel.inputChar('1');
        panel.inputChar('1'); // 2次错误
        panel.inputChar('2');
        panel.inputChar('2');
        panel.inputChar('2');
        panel.inputChar('2'); // 3次错误，锁定
    }
}